import { addLookupApi, editLookupApi } from "@/api/configCenter/dataDictManagement";
import FormHook from "@/hook/formHook";
import { GeneraDataType } from "@/interface";
import { message } from "ant-design-vue";
export default function OperationHook(state: GeneraDataType) {
  const { formState, formDataRef, emit, type, passData } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const onFinish = () => {
    let requestFn;
    const params = {
      ...formState
    };
    if (type.value === 1) {
      requestFn = addLookupApi;
      Reflect.deleteProperty(params, "id");
    } else {
      requestFn = editLookupApi;
      params.id = passData.value.id;
    }
    requestFn(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!");
        emit("submit");
      }
    });
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const addHandle = () => {
    formState.itemList.push({
      name: undefined,
      code: undefined,
      status: 1
    });
  };
  const removeHandle = (index: number) => {
    formState.itemList.splice(index, 1);
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    addHandle,
    removeHandle,
    resetFields,
    validateZhInputHandle
  };
}
